package com.csdj.rest.service;

import com.csdj.beans.entity.Menu;
import com.csdj.beans.interfaces.user.IUserService;
import com.csdj.common.constant.CacheConstant;
import com.csdj.rest.mapper.MenuMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Service
@CacheConfig(cacheNames = CacheConstant.CACHE_CONFIG)
public class MenuService {

    @Resource
    private MenuMapper menuMapper;
//    @DubboReference
    private IUserService userService;


    @Cacheable(key = "#root.methodName+#curRole")
    public List<Menu> getMenus(String curRole){
        //查出所有的菜单
        List<Menu> list = menuMapper.findByTopId();

        //根据权限过滤
        if(list != null && list.size()>0){
            //用户的角色
//            String curRole = userService.getCurUserRole(uid);
            return list.stream().filter(item ->{
                //区分url是否为空
                if(StringUtils.hasText(item.getUrl())){
                    //url 不为空
                    return checkRole(item.getRole(),curRole);
                }
                //url为空，有子菜单， 过滤子菜单
                List<Menu> sub = filterByRole(item.getSubs(),curRole);
                //把过滤好的子菜单集合替换父菜单上原子菜单集合
                if(sub != null){
                    item.setSubs(sub);
                    return true;
                }
                return false;//舍弃空的父菜单

            }).collect(Collectors.toList());
        }
        return null;
    }

    public List<Menu> getMenus(){
//        String role = userService.getCurUserRole(uid);
        return getMenus("admin");
    }

    public List<Menu> getMenusOld(int uid){
        final List<Menu> allList = menuMapper.findByTopId();
        String curRole = userService.getCurUserRole(uid);
        final Iterator<Menu> iterator = allList.iterator();
        while(iterator.hasNext()){
            Menu menu = iterator.next();
            //判断url是否为空
            if(StringUtils.hasText(menu.getUrl())){
                //有url 判断权限
                if(checkRole(menu.getRole(),curRole)){
                    continue;
                }else{
                    allList.remove(menu);//? 越界
                }
            }else{
                //没有url  判断子菜单
                menu.setSubs(filterByRole(menu.getSubs(),curRole));
                if(menu.getSubs() == null || menu.getSubs().size() == 0){
                    allList.remove(menu);//去掉空父菜单
                }
            }
        }
        //权限判断完成
        return allList;


    }


    /**
     * 角色数组对比
     * @param roles
     * @param role
     * @return
     */
    private boolean checkRole(String roles,String role){
        if(StringUtils.hasText(roles)){
            String[] roleArray = roles.split(",");
            for(String r: roleArray){
                if(r.equals(role))return true;
            }
        }
        return false;
    }

    /**
     * 过滤菜单
     * @param menus
     * @param role
     * @return
     */
    private List<Menu> filterByRole(List<Menu> menus,String role){
        if(menus != null && menus.size()>0){
            return menus.stream().filter(menu -> {
                return checkRole(menu.getRole(),role);
            }).collect(Collectors.toList());
        }
//        List<Menu> newList = new ArrayList<>();
//        for(Menu m : menus){
//            if(checkRole(m.getRole(),role)){
//                newList.add(m);
//            }
//        }
//        return newList;


        return null;
    }
}
